package com.matang;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions.Callback;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage.QuotaUpdater;
import android.webkit.WebView;
import android.widget.EditText;

/**
 * http://618119.com/archives/2010/12/20/199.html
 */

// ****************************************************************************
public class MyWebChromeClient extends WebChromeClient {
	// ������ݿ�����
	@Override
	public void onExceededDatabaseQuota(String url, String databaseIdentifier,
			long quota, long estimatedDatabaseSize, long totalQuota,
			QuotaUpdater quotaUpdater) {
		// TODO Auto-generated method stub
		quotaUpdater.updateQuota(estimatedDatabaseSize * 2);
//		super.onExceededDatabaseQuota(url, databaseIdentifier, quota,
//				estimatedDatabaseSize, totalQuota, quotaUpdater);
	}

	@Override
	public void onGeolocationPermissionsShowPrompt(String origin,
			Callback callback) {
		// TODO ����Ȩ��(ͬ����WebChromeClient��ʵ��)
		callback.invoke(origin, true, false);
		super.onGeolocationPermissionsShowPrompt(origin, callback);
	}

	public ProgressDialog showdialog;

	public MyWebChromeClient(ProgressDialog showdialog) {
		this.showdialog = showdialog;
	}

	@Override
	public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
		// TODO Auto-generated method stub
		if (consoleMessage.message().contains("Uncaught ReferenceError")) {
			// do something...
			Log.e("MyWebChromeClient---error", consoleMessage.message());
		}
		return super.onConsoleMessage(consoleMessage);
	}

	@Override
	public void onCloseWindow(WebView window) {
		super.onCloseWindow(window);
	}

	@Override
	public boolean onCreateWindow(WebView view, boolean dialog,
			boolean userGesture, Message resultMsg) {
		return super.onCreateWindow(view, dialog, userGesture, resultMsg);
	}

	/**
	 * ����Ĭ�ϵ�window.alertչʾ���棬����title����ʾΪ��������file:////��
	 */
	public boolean onJsAlert(WebView view, String url, String message,
			JsResult result) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(
				view.getContext());

		builder.setTitle("�Ի���").setMessage(message)
				.setPositiveButton("ȷ��", null);

		// ����Ҫ�󶨰����¼�
		// ����keycode����84֮��İ���
		builder.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				Log.v("onJsAlert", "keyCode==" + keyCode + "event=" + event);
				return true;
			}
		});
		// ��ֹ��Ӧ��back����¼�
		builder.setCancelable(false);
		AlertDialog dialog = builder.create();
		dialog.show();
		result.confirm();// ��Ϊû�а��¼�����Ҫǿ��confirm,����ҳ�������ʾ�������ݡ�
		return true;
		// return super.onJsAlert(view, url, message, result);
	}

	@Override
	public void onProgressChanged(WebView view, int newProgress) {
		// TODO Auto-generated method stub
		showdialog.setTitle("ҳ������У����Ժ�..." + newProgress + "%");
		showdialog.setProgress(newProgress * 100);
		if (newProgress == 100) {

			showdialog.setTitle(R.string.app_name);
		}
	}

	public boolean onJsBeforeUnload(WebView view, String url, String message,
			JsResult result) {
		return super.onJsBeforeUnload(view, url, message, result);
	}

	/**
	 * ����Ĭ�ϵ�window.confirmչʾ���棬����title����ʾΪ��������file:////��
	 */
	public boolean onJsConfirm(WebView view, String url, String message,
			final JsResult result) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(
				view.getContext());
		builder.setTitle("�Ի���").setMessage(message)
				.setPositiveButton("ȷ��", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						result.confirm();
					}
				}).setNeutralButton("ȡ��", new OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						result.cancel();
					}
				});
		builder.setOnCancelListener(new OnCancelListener() {
			@Override
			public void onCancel(DialogInterface dialog) {
				result.cancel();
			}
		});

		// ����keycode����84֮��İ�����ⰴ����¶Ի�����Ϣ��ҳ���޷��ٵ����Ի��������
		builder.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				Log.v("onJsConfirm", "keyCode==" + keyCode + "event=" + event);
				return true;
			}
		});
		// ��ֹ��Ӧ��back����¼�
		// builder.setCancelable(false);
		AlertDialog dialog = builder.create();
		dialog.show();
		return true;
		// return super.onJsConfirm(view, url, message, result);
	}

	/**
	 * ����Ĭ�ϵ�window.promptչʾ���棬����title����ʾΪ��������file:////��
	 * window.prompt('��������������ַ', '618119.com');
	 */
	public boolean onJsPrompt(WebView view, String url, String message,
			String defaultValue, final JsPromptResult result) {
		final AlertDialog.Builder builder = new AlertDialog.Builder(
				view.getContext());

		builder.setTitle("�Ի���").setMessage(message);

		final EditText et = new EditText(view.getContext());
		et.setSingleLine();
		et.setText(defaultValue);
		builder.setView(et).setPositiveButton("ȷ��", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				result.confirm(et.getText().toString());
			}

		}).setNeutralButton("ȡ��", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				result.cancel();
			}
		});

		// ����keycode����84֮��İ�����ⰴ����¶Ի�����Ϣ��ҳ���޷��ٵ����Ի��������
		builder.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(DialogInterface dialog, int keyCode,
					KeyEvent event) {
				Log.v("onJsPrompt", "keyCode==" + keyCode + "event=" + event);
				return true;
			}
		});

		// ��ֹ��Ӧ��back����¼�
		// builder.setCancelable(false);
		AlertDialog dialog = builder.create();
		dialog.show();
		return true;
		// return super.onJsPrompt(view, url, message, defaultValue,
		// result);
	}

	// @Override
	// public void onProgressChanged(WebView view, int newProgress) {
	// super.onProgressChanged(view, newProgress);
	// }

	@Override
	public void onReceivedIcon(WebView view, Bitmap icon) {
		super.onReceivedIcon(view, icon);
	}

	@Override
	public void onReceivedTitle(WebView view, String title) {
		super.onReceivedTitle(view, title);
	}

	@Override
	public void onRequestFocus(WebView view) {
		super.onRequestFocus(view);
	}
}

// @Override
// public void onProgressChanged(WebView view, int newProgress) {
// // TODO Auto-generated method stub
// super.onProgressChanged(view, newProgress);
// }
//
// @Override
// public void onReceivedTitle(WebView view, String title) {
// // TODO Auto-generated method stub
// super.onReceivedTitle(view, title);
// }
//
// @Override
// public void onReceivedIcon(WebView view, Bitmap icon) {
// // TODO Auto-generated method stub
// super.onReceivedIcon(view, icon);
// }
//
// @Override
// public void onReceivedTouchIconUrl(WebView view, String url,
// boolean precomposed) {
// // TODO Auto-generated method stub
// super.onReceivedTouchIconUrl(view, url, precomposed);
// }
//
// @Override
// public void onHideCustomView() {
// // TODO Auto-generated method stub
// super.onHideCustomView();
// }
//
// @Override
// public boolean onCreateWindow(WebView view, boolean isDialog,
// boolean isUserGesture, Message resultMsg) {
// // TODO Auto-generated method stub
// return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
// }
//
// @Override
// public void onRequestFocus(WebView view) {
// // TODO Auto-generated method stub
// super.onRequestFocus(view);
// }
//
// @Override
// public void onCloseWindow(WebView window) {
// // TODO Auto-generated method stub
// super.onCloseWindow(window);
// }
//
// @Override
// public boolean onJsAlert(WebView view, String url, String message,
// JsResult result) {
// // TODO Auto-generated method stub
// return super.onJsAlert(view, url, message, result);
// }
//
// @Override
// public boolean onJsConfirm(WebView view, String url, String message,
// JsResult result) {
// // TODO Auto-generated method stub
// return super.onJsConfirm(view, url, message, result);
// }
//
// @Override
// public boolean onJsPrompt(WebView view, String url, String message,
// String defaultValue, JsPromptResult result) {
// // TODO Auto-generated method stub
// return super.onJsPrompt(view, url, message, defaultValue, result);
// }
//
// @Override
// public boolean onJsBeforeUnload(WebView view, String url, String message,
// JsResult result) {
// // TODO Auto-generated method stub
// return super.onJsBeforeUnload(view, url, message, result);
// }
//
// @Override
// public void onGeolocationPermissionsShowPrompt(String origin,
// Callback callback) {
// // TODO Auto-generated method stub
// super.onGeolocationPermissionsShowPrompt(origin, callback);
// }
//
// @Override
// public void onGeolocationPermissionsHidePrompt() {
// // TODO Auto-generated method stub
// super.onGeolocationPermissionsHidePrompt();
// }
//
// @Override
// public boolean onJsTimeout() {
// // TODO Auto-generated method stub
// return super.onJsTimeout();
// }
//
// @Override
// public void onConsoleMessage(String message, int lineNumber, String sourceID)
// {
// // TODO Auto-generated method stub
// super.onConsoleMessage(message, lineNumber, sourceID);
// }
//
// @Override
// public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
// // TODO Auto-generated method stub
// return super.onConsoleMessage(consoleMessage);
// }
//
// @Override
// public Bitmap getDefaultVideoPoster() {
// // TODO Auto-generated method stub
// return super.getDefaultVideoPoster();
// }
//
// @Override
// public View getVideoLoadingProgressView() {
// // TODO Auto-generated method stub
// return super.getVideoLoadingProgressView();
// }
//
// @Override
// public void getVisitedHistory(ValueCallback<String[]> callback) {
// // TODO Auto-generated method stub
// super.getVisitedHistory(callback);
// }
//
// @Override
// public void onExceededDatabaseQuota(String url, String databaseIdentifier,
// long quota, long estimatedDatabaseSize, long totalQuota,
// QuotaUpdater quotaUpdater) {
// // TODO Auto-generated method stub
// super.onExceededDatabaseQuota(url, databaseIdentifier, quota,
// estimatedDatabaseSize, totalQuota, quotaUpdater);
// }
//
// @Override
// public void onReachedMaxAppCacheSize(long requiredStorage, long quota,
// QuotaUpdater quotaUpdater) {
// // TODO Auto-generated method stub
// super.onReachedMaxAppCacheSize(requiredStorage, quota, quotaUpdater);
// }
//
// private WebChromeClient mWrappedClient;
//
// public MyWebChromeClient(WebChromeClient wrappedClient) {
// mWrappedClient = wrappedClient;
// }
//
// public void openFileChooser(ValueCallback<Uri> uploadFile) {
// ((MyWebChromeClient) mWrappedClient).openFileChooser(uploadFile);
// }
//
// }
